package leetcode.doublepointer;


import java.util.Arrays;

//977. 有序数组的平方
public class SortedSquares {

    public static void main(String[] args) {
        int[] data = {-7,-3,2,3,11};
        int[] ints = sortedSquares2(data);
        System.out.println(Arrays.toString(ints));

    }
    public static int[] sortedSquares(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            nums[i] = nums[i]*nums[i];
        }
        Arrays.sort(nums);
        return nums;
    }

    //双指针   ---倒着放入数组
    public static int[] sortedSquares2(int[] nums) {
        int[]  f = new int[nums.length];
        int index = nums.length-1;
        int p = 0;
        int q = nums.length-1;
        while (p<=q){
            if (nums[p]*nums[p]>nums[q]*nums[q]){
                f[index--] = nums[p]*nums[p];
                p++;
            }else {
                f[index--] = nums[q]*nums[q];
                q--;
            }
        }
        return f;
    }
}
